<template>
  <div id="one-box">
    <div class="header">
      <router-link :to="'/personalSetting/personal?id='+userInfo.profile.userId" class="screen-back">个人设置</router-link>
      <span class="iconfont header-icon">&#xe61d;</span>
      <span>更换头像</span>
    </div>
    <div class="control">
      <button class="white-button" @click="fileLoad">上传头像</button>
      <p class="margin-left">支持jpg、png、bmp格式的图片, 且文件小于20M</p>
    </div>
    <div class="content">
      <div class="left">
        <ImgCutter
          class="img-edit"
          ref="imgCutterModal"
          label="选择一张本地图片,编辑后上传"
          :crossOrigin="true"
          crossOriginHeader="*"
          rate=""
          toolBgc="none"
          :isModal="false"
          :showChooseBtn="true"
          :lockScroll="true"
          :boxWidth="322"
          :boxHeight="322"
          :cutWidth="180"
          :cutHeight="180"
          :sizeChange="true"
          :moveAble="true"
          :originalGraph="false"
          WatermarkTextFont="12px Sans-serif"
          WatermarkTextColor="#00ff00"
          :WatermarkTextX="0.95"
          :WatermarkTextY="0.95"
          :smallToUpload="true"
          @cutDown="cutDown"
        >
        </ImgCutter>
      </div>
      <div class="right">
        <p class="right-title">头像预览</p>
        <div class="big-img">
          <div
            class="img"
            v-loading="imgLoading"
            :element-loading-background="ELEMENT_LOADING_BACKGROUND"
            element-loading-spinner="el-icon-loading"
            :element-loading-text="ELEMENT_LOADING_TEXT"
          >
            <img v-if="userImgNew" :src="userImgNew" alt="" />
            <img
              v-else-if="userDetails"
              :src="userDetails.profile.avatarUrl"
              alt=""
            />
            <img v-else :src="userInfo.profile.avatarUrl" alt="" />
          </div>
          <p>大尺寸头像（180x180像素）</p>
        </div>
        <div class="small-img">
          <div
            class="img"
          >
            <img v-if="userImgNew" :src="userImgNew" alt="" />
            <img
              v-else-if="userDetails"
              :src="userDetails.profile.avatarUrl"
              alt=""
            />
            <img v-else :src="userInfo.profile.avatarUrl" alt="" />
          </div>
          <p>小尺寸头像（40x40像素）</p>
        </div>
      </div>
    </div>
    <!-- <div class="btn btn-primary" slot="open">Crop local pictures</div> -->
  </div>
</template>

<script>
import {getFileDesc} from '@/utils/getFileDesc.js'
import { userDetail } from "@/api";
import { mapGetters, mapActions } from "vuex";
import ImgCutter from "vue-img-cutter";
import { avatarUpload } from "@/api";
export default {
  name: "replacePicture",
  data() {
    return {
      imgLoading: false,
    };
  },
  components: {
    ImgCutter,
  },
  computed: {
    ...mapGetters(["userInfo", "userDetails", "userImgNew"]),
  },
  created() {
    this.getUserDetails();
  },
  methods: {
    ...mapActions("user", [
      "SET_CHANGE_USER_Details",
      "SET_CHANGE_USER_USER_IMG_NEW",
    ]),
    cutDown(val) {
      this.imgLoading = true;
      let formData = new FormData();
      let file = getFileDesc(val.file);
      formData.append("imgFile", file.file);
      avatarUpload(formData).then((data) => {
        this.SET_CHANGE_USER_USER_IMG_NEW(data.data.url);
        this.imgLoading = false;
        // 重新获取用户数据  不能实时更新头像  优先使用上传接口返回地址
        console.log(
          "%c重新获取用户数据  不能实时更新头像  优先使用上传接口返回地址",
          "background:#000;color:#fff;"
        );
        this.getUserDetails();
      });
    },
    fileLoad() {
      document.querySelector(".btn.btn-warning.btn-xs").click();
    },
    getUserDetails() {
      userDetail({ uid: this.userInfo.profile.userId }).then((data) => {
        this.SET_CHANGE_USER_Details(data);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  .left {
    // padding-right:30px;
    // border-right:1px solid #e2e2e2;
    margin-right: 50px;
  }
  .right {
    margin-left: 50px;
    .right-title {
      font-size: 12px;
      color: #333;
    }
    .big-img {
      margin-top: 20px;
      font-size: 12px;
      color: #999;
      .img,
      img {
        width: 180px;
      }
      p {
        margin: 10px 0;
      }
    }
    .small-img {
      margin-top: 20px;
      font-size: 12px;
      color: #999;
      .img,
      img {
        width: 40px;
      }
      p {
        margin: 10px 0;
      }
    }
  }
}
.control {
  margin: 25px 0;
  display: flex;
  font-size: 12px;
  line-height: 30px;
  .white-button {
    cursor: pointer;
    width: 95px;
    height: 30px;
  }
  .margin-left {
    margin-left: 18px;
    color: #999;
  }
}
.header {
  font-size: 15px;
  font-weight: 550;
  color: #333;
  padding-bottom: 6px;
  border-bottom: 1px solid #cbcbcb;
  .screen-back {
    color: #0c73c2;
  }
  .header-icon {
    font-size: 12px;
    line-height: 16px;
    margin: 0 5px;
  }
}
.img-edit {
  /deep/ .canvasSelectBox {
    padding-right: 50px;
    border-right: 1px solid #e2e2e2;
  }
  /deep/.copyright {
    display: none !important;
  }
  /deep/ .tips {
    width: 200px;
    .btn.btn-warning.btn-xs {
      width: 100%;
    }
  }
  /deep/ .i-dialog-footer > span {
    display: none;
  }
  /deep/ .btn-group.fr {
    display: flex;
    width: 100%;
    span:nth-of-type(1) button {
      line-height: 30px;
      border-radius: 8px;
      width: 80px;
      height: 30px;
      border: 1px solid #c4c4c4;
      background-image: linear-gradient(#ffffff, #f1f1f1);
      color: #333;
      border-radius: 3px;
    }

    span:nth-of-type(2) button {
      line-height: 30px;
      border-radius: 8px;
      width: 80px;
      height: 30px;
      background-image: linear-gradient(#4da1e0, #1d6ebe);
      border: 1px solid #3984ce;
      color: #fff;
      border-radius: 3px;

      &:hover {
        color: #fff !important;
      }
    }
  }
}
</style>